﻿using AutoMapper;
using NetTaste;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using VM.Common.Result;
using VM.Entity.EntityDto.People;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.People;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IDAL.People;

namespace VM.DAL.People
{
    public class PeopleInfoDAL : BaseDAL, IPeoPleInfoDAL
    {

        protected ISqlSugarClient _Client { get; set; }
        protected IMapper _IMapper { get; set; }


        public PeopleInfoDAL(ISqlSugarClient client, IMapper iMapper) : base(client)
        {
            _Client = client;
            _IMapper = iMapper;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int addPeoPleInfo(people_info obj)
        {
            int Res = _Client.Insertable(obj)
                       .ExecuteCommand();
            return Res;
        }




        public PagingData<getPeopleInfoListDto> getPeopleInfoList(int pageIndex, int pageSize, string name, int typeId,int UserId)
        {
            var listRes = _Client.Queryable<people_info>()
                    .Includes(a => a.createUser)
                    .Includes(a => a.updateUser)

            .WhereIF(name != "" && name != null, a => a.name.Contains(name))
            .WhereIF(typeId != 0, a => a.typeId == typeId)
            .Where(a => a.billStatus != 999)
            .Where( a => a.createUserId == UserId)
            ;

            PagingData<people_info> result = new PagingData<people_info>()
            {
                dataList = listRes.ToPageList(pageIndex, pageSize),
                pageIndex = pageIndex,
                pageSize = pageSize,
                recordCount = listRes.Count(),
            };
            PagingData<getPeopleInfoListDto> resend = _IMapper.Map<PagingData<people_info>, PagingData<getPeopleInfoListDto>>(result);
            return resend;

        }


        public getPeopleInfoDetailDto getPeopleInfoDetailById(int id)
        {
            people_info Res = _Client.Queryable<people_info>()
                           .Includes(a => a.createUser)
                           .Includes(a => a.updateUser)
                           .InSingle(id);//
            getPeopleInfoDetailDto EndRes = _IMapper.Map<people_info, getPeopleInfoDetailDto>(Res);

            return EndRes;
        }

        public int updatePeoPleInfo(people_info peopleInfo)
        {
            int  result = _Client.Updateable(peopleInfo)
                .UpdateColumns(it => new { it.phone, it.idCard ,it.name})
                .IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true)
                .ExecuteCommand();
            return result;

        }

        public int deletePeoPleInfo(int id)
        {
            people_info peopleInfo = new people_info()
            {
                id=id,
                billStatus=999
            };
            int result = _Client.Updateable(peopleInfo)
                .UpdateColumns(it => new { it.billStatus})
                .IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true)
                .ExecuteCommand();
            return result;

        }
    }
}
